package com.qf.springboot2203.config;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.springboot2203.entity.User;
import com.qf.springboot2203.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;

public class CustomRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        User user = (User) principalCollection.getPrimaryPrincipal();

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

        simpleAuthorizationInfo.addRoles(user.getRoses());

        simpleAuthorizationInfo.addStringPermissions(user.getPerms());
        return simpleAuthorizationInfo;
    }


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        User user = new User();

        user.setPassword("123");

        user.setUsername("zhangsan");

        user.setPerms(Arrays.asList("ok"));

        user.setRoses(Arrays.asList("admin"));

        //需要把数据库中的用户的密码返回shiro
        //注意 第一个参数可以放入从数据库中查询粗来的完整的
        //的user对象,也可以指放入用户名，这里会影响后续从shiro中获取用户身份证信息
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(user, user.getPassword(),
                ByteSource.Util.bytes("123"),
                this.getName());

        return simpleAuthenticationInfo;
    }
}
